package com.class5.springboot.util;

import com.class5.springboot.entity.TbXtUser;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.*;

/**
 * Created by YT on 2018/5/16.
 */
public class BeanCopyUtils {

    /**
     * 复制源对象非空数据到目标对象
     *
     * @param source
     * @param target
     */
    public static void copyNoNullValue(Object source, Object target) {
        Field[] fields = source.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(source);
                if (value != null && !Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers())) {
                    field.set(target, value);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("对象复制时失败",e);
        }
    }

    /**
     * Map控制null转化为“”
     * @param source
     * @param target
     */
    public static void handleNullForMap(Map<String,Object> map) {
        Set key = map.keySet();
        Iterator<String> iterator =  key.iterator();
        List<String> keyList = new ArrayList();
        while(iterator.hasNext()){
            keyList.add(iterator.next());
        }
        for(int i=0;i<keyList.size();i++){
            if(map.get(keyList.get(i))==null){
                map.put(keyList.get(i),"");
            }
        }
    }

    /**
     * 控制转化为“”(相同对象类型)
     *
     * @param source
     * @param target
     */
    public static void handleNull(Object source, Object target) {
        Field[] fields = source.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(source);
                if (value != null && !Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers())) {
                    field.set(target, value);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("对象复制时失败",e);
        }
    }

    /**
     * 复制两个不同对象数据  字段（类型 名称） 必须一致
     * @param source
     * @param target
     */
    public static void copyNoEqClass(Object source, Object target) {
        Field[] fields = getFields(new ArrayList<Field>(),source.getClass());
        try {
            for (Field field : fields) {
                field.setAccessible(true);
                Object value = field.get(source);
                if(!Modifier.isStatic(field.getModifiers()) && !Modifier.isFinal(field.getModifiers())){
                    if (value != null) {
                        Field targetField = getField(target.getClass(),field.getName());
                        if(targetField!=null && targetField.getType().equals(field.getType())){
                            targetField.setAccessible(true);
                            targetField.set(target, value);
                        }
                    }else{
                        Field targetField = getField(target.getClass(),field.getName());
                        if(targetField!=null && targetField.getType().equals(field.getType())){
                            targetField.setAccessible(true);
                            if(field.getType().getName().equals("java.lang.String")){

                            }
                            targetField.set(target, "");
                        }
                    }
                }

            }
        } catch (Exception e) {
            throw new RuntimeException("对象复制时失败",e);
        }
    }

    public static  Field[] getFields(List<Field> fields,Class clz){
        fields.addAll(Arrays.asList(clz.getDeclaredFields()));
        if(clz!=Object.class){
            return getFields(fields,clz.getSuperclass());
        }
        return fields.toArray(new Field[fields.size()]);
    }

    public static Field getField(Class clz,String fieldName){
        try{
            return clz.getDeclaredField(fieldName);
        }
        catch(Exception e){
        }
        if(clz != Object.class){
            return getField(clz.getSuperclass(),fieldName);
        }
        return null;
    }

    public static void main(String[] args) {
        TbXtUser tb1 = new TbXtUser();
        TbXtUser tb2 = new TbXtUser();
        tb1.setEmail(null);
        tb1.setPhone(null);
        tb1.setUserName("1111");
        copyNoEqClass(tb1,tb2);
        System.out.println(tb1.getEmail());
    }
}
